<?php

/* Associated files: courseprefix.xml and librarianname.xml
Both files should be in the same directory as this script... or edit simplexml_load_file below
Use test.html to test different several courseid that could be sent from Blackboard
Code License: GNU General Public License v3
Content License: Creative Commons 3.0 BY
More info: http://code.google.com/p/library2blackboard/ */

//Error Messages
$nocourseid = 'That prefix is either misconfigured or is not listed in courseprefix.xml';
$noxmlfile = 'Missing XML file. Contact the administrator.';

//XML Filenames
$course_xml_file = 'courseprefix.xml';
$lib_xml_file = 'librarianname.xml';

//Input Filtering
$clean = array();

//Grab courseid and validate courseid. Courseid is generated in Blackboard using @X@course.course_id@X@ template variable
if (!isset($_GET['courseid'])) {
	exit($nocourseid);
}

if (preg_match('/^[a-zA-Z0-9_()]{1,26}$/', $_GET['courseid'])) {
	$clean['course_id'] = $_GET['courseid'];
} else {
	exit($nocourseid);
}

//Get Course Info
$course_info = get_course_info($clean['course_id']);
if (!$course_info) {
	exit($nocourseid);
}

//Get XML info
if ((file_exists($course_xml_file)) && (file_exists($lib_xml_file))) {
	$xml_info = get_xml_info($course_xml_file, $lib_xml_file, $course_info['course_prefix']);
} else {
	exit($noxmlfile);
}

//Exit if prefix not found in xml file
if ($xml_info['result_librarian'] == "") {
	print "ARGH!<BR>";
	exit($nocourseid);
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>UT Libraries in BlackBoard</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>

<p>Course Prefix and Number: <?php print $course_info['course_prefix']; ?> <?php print $course_info['course_number']; ?></p>

<p>Librarian Name: <?php print $xml_info['result_contactname']; ?>

<p>Email Address: <a href="mailto:<?php print $xml_info['result_email']; ?>"><?php print $xml_info['result_email']; ?></a></p>

<p>Subject Guide: <a href="<?php print $xml_info['result_subject_guide']; ?>">Course Resources</a></p>

</body>
</html>
<?php

//Functions
function get_course_info($crs_id)
{
	if ($crs_id == "") {
		return 0;
	}

	$crs_inf[''] = "";
	$crs_inf[''] = "";

	//Explode elements of $course_id
	$crs_elements = explode("_", $crs_id);

	//Determine course prefix and account for 2-letter prefixes w/ extra underscore
	$crs_prefix_array = count($crs_elements);

	if ($crs_prefix_array == 5) {
    	$crs_inf['course_prefix'] = $crs_elements[3]; //5 segments in array w 3-letter prefix
	} elseif ($crs_prefix_array == 6) {
    	$crs_inf['course_prefix'] = $crs_elements[3].$crs_elements[4]; //6 segments in array, 2-letter prefix
	} else {
    	return 0;
    }
    
    //Determine Course Number and account for 2-letter prefixes w/ extra underscore
	if ($crs_prefix_array == 5) {
	    $crs_inf['course_number'] = $crs_elements[4]; //5 segments in array, 3-letter prefix
	} elseif ($crs_prefix_array == 6) {
    	$crs_inf['course_number'] = $crs_elements[5]; //6 segments in array, 2-letter prefix
	} else {
    	return 0;
    }
    
    return $crs_inf;
}

function get_xml_info($course_xml, $lib_name_xml, $course_prefix)
{
	if ($course_xml == "") {
		return 0;
	}
	if ($lib_name_xml == "") {
		return 0;
	}

	$xml_inf['return_subjectguide'] = "";
	$xml_inf['return_librarian'] = "";
	$xml_inf['result_email'] = "";
	$xml_inf['result_contactname'] = "";

	//Grab subject_guide and librarian from prefix.xml
	$xml_prefix = simplexml_load_file($course_xml);
	$xml_contactname = simplexml_load_file($lib_name_xml);

	$return_subjectguide = $xml_prefix->xpath("/root/courseprefix[prefix='$course_prefix']/subject_guide");
	$return_librarian = $xml_prefix->xpath("/root/courseprefix[prefix='$course_prefix']/librarian");

	$xml_inf['result_subject_guide'] = $return_subjectguide[0];
	$xml_inf['result_librarian'] = $return_librarian[0];
	$result_librarian = $return_librarian[0];

	//Grab Name and email from name.xml using $return_librarian
	$return_email = $xml_contactname->xpath("/root/contactname[name='$result_librarian']/email");
	$return_contactname = $xml_contactname->xpath("/root/contactname[name='$result_librarian']/name");

	$xml_inf['result_email'] = $return_email[0];
	$xml_inf['result_contactname'] = $return_contactname[0];
	
	return $xml_inf;
}

?>